package com.sg.hms.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.sg.hms.Model.User;
import com.sg.hms.Service.ApplicationService;
import com.sg.hms.ServiceImpl.ApplicationServiceImpl;
/***
 * 填写申请控制
 * @author 吴磊
 *
 */
public class AddApplicationContriller extends HttpServlet{
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("UTF-8");
		String text=req.getParameter("text");
		User user=(User)req.getSession().getAttribute("currentuser");
		Date date=new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  
	    String dateNowStr = sdf.format(date);
	    Date sDate=null;
	    try {
	         sDate=sdf.parse(dateNowStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    java.sql.Date needdate=new java.sql.Date(sDate.getTime());
	    String state="已提交";
	    ApplicationService app=new ApplicationServiceImpl();
	    int temp=app.addApplication(user.getuId(), text, state, needdate);
	    if (temp==0) {
			req.setAttribute("next", "失败");
		}else {
			req.setAttribute("next", "成功");
		}
	    req.getRequestDispatcher("allapplication").forward(req, resp);
	}

}
